package cn.ztr.ymcc.web.controller;

import cn.ztr.ymcc.dto.CourseDTO;
import cn.ztr.ymcc.service.ICourseService;
import cn.ztr.ymcc.domain.Course;
import cn.ztr.ymcc.query.CourseQuery;
import cn.ztr.ymcc.result.JsonResult;
import cn.ztr.ymcc.result.PageList;
import cn.ztr.ymcc.utils.LoginContextUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;
    @Autowired
    private LoginContextUtils loginContextUtils;

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody CourseDTO course, HttpServletRequest request) throws Exception {
        if(course.getCourse().getId()!=null){
            courseService.updateById(course);
        }else{
            //解析token
            String token = request.getHeader("jwtToken");
            //添加登录账号的id和用户名
            course.getCourse().setLoginId(loginContextUtils.getUserId(token));
            course.getCourse().setLoginUserName(loginContextUtils.getUserName(token));
            courseService.save(course);
        }
        return JsonResult.me();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        courseService.removeById(id);
        return JsonResult.me();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.me().setData(courseService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.me().setData(courseService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPageNo(), query.getPageSize());
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(query.getKeyword()),"name",query.getKeyword())
                .orderBy(StringUtils.hasText(query.getOrderType()),"acs".equals(query.getOrderType()),query.getOrderField());

        page = courseService.page(page,wrapper);
        return JsonResult.me().setData(new PageList<Course>(page.getTotal(), page.getRecords()));
    }

    @GetMapping("/getOtherDetail/{id}")
    public JsonResult getOtherDetail(@PathVariable("id")Long id){
        return courseService.getOtherDetail(id);
    }

    /**
     * 课程上架
     * @param courses
     * @return
     */
    @PostMapping("/onLineCourse")
    public JsonResult onLineCourse(@RequestBody List<Course> courses){
        courseService.onLineCourse(courses);
        return JsonResult.me();
    }

    /**
     * 根据课程id获取讲师信息
     * @param id
     * @return
     */
    @GetMapping("/getTeacherCourseId/{id}")
    public JsonResult getTeacherCourseId(@PathVariable("id")Long id){
        return courseService.getTeacherCourseId(id);
    }



}
